﻿using System;
using System.Collections.Generic;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class EmployeeBiz
    {
        #region Properties

        private readonly IEmployeeDao _employeeDao;
        private readonly IRoleDao _roleDao;

        #endregion

        #region Constructors

        public EmployeeBiz()
        {
            _employeeDao = new EmployeeDao();
            _roleDao = new RoleDao();
        }

        public EmployeeBiz(IEmployeeDao employeeDao, IRoleDao roleDao)
        {
            _employeeDao = employeeDao;
            _roleDao = roleDao;
        }

        #endregion

        #region Methods

        public Employee Logon(string userName, string password)
        {
            var employee = _employeeDao.GetEmployeeByUserNamePassword(userName, password);
            if (employee != null)
            {
                employee.Roles = _roleDao.GetRolesOfEmployee(employee.Id);
            }

            return employee;
        }

        public Employee GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _employeeDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public Employee CreateEmployee(Employee employee)
        {
            // validate inputs
            if (employee == null) return null;
            if (string.IsNullOrEmpty(employee.Code) || string.IsNullOrEmpty(employee.Name)) return null;

            // is existed username
            var existedEmp = _employeeDao.GetByUserName(employee.UserName);
            if (existedEmp != null)
                throw new ApplicationException("UserNameIsExisted");

            try
            {
                return _employeeDao.AddEmployee(employee);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _employeeDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _employeeDao.GetCurrentIdentity() + 1;
        }

        public void UpdateEmployee(Employee employee)
        {
            if (employee == null) return;

            _employeeDao.UpdateEmployee(employee);
        }

        public object GetAllEmployeesWs()
        {
            try
            {
                var peopleList = _employeeDao.GetAllEmployees();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public List<Employee> GetAllEmployees()
        {
            return _employeeDao.GetAllEmployees();
        } 

        public object CreateEmployeeWs(Employee employee)
        {
            // validate inputs
            if (employee == null) return null;
            if (string.IsNullOrEmpty(employee.Code) || string.IsNullOrEmpty(employee.Name)) return null;

            try
            {
                var addedEmployee = _employeeDao.AddEmployee(employee);
                return new { Result = "OK", Record = addedEmployee };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateEmployeeWs(Employee employee)
        {
            if (employee == null) return null;
            try
            {
                _employeeDao.UpdateEmployee(employee);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteEmployeeWs(int employeeId)
        {
            try
            {
                _employeeDao.DeleteEmployee(employeeId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public Employee GetByName(string userName)
        {
            var employee = _employeeDao.GetByUserName(userName);
            if (employee != null)
            {
                employee.Roles = _roleDao.GetRolesOfEmployee(employee.Id);
            }

            return employee;
        }

        #endregion

        #region Roles

        public List<Role> GetAllRoles()
        {
            return _roleDao.GetAll();
        }

        public List<Employee> GetEmployeesOfRole(int roleId)
        {
            return _roleDao.GeGetEmployeesOfRole(roleId);
        }
        
        public Role GetRoleById(int roleId)
        {
            return roleId <= 0 ? null : _roleDao.GetById(roleId);
        }

        public void AssignToRole(int roleId, List<int> assignedIds)
        {
            if (roleId <= 0) return;

            _roleDao.AssignToRole(roleId, assignedIds);
        }

        public List<Role> GetRolesOfEmployee(int employeeId)
        {
            return _roleDao.GetRolesOfEmployee(employeeId);
        } 

        #endregion

        #region Helpers

        #endregion
    }
}
